// 54
package org.hwadee.controller;

import org.hwadee.entity.Comment;
import org.hwadee.entity.Post;
import org.hwadee.entity.User;
import org.hwadee.service.CommentService;
import org.hwadee.service.PostService;
import org.hwadee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backIndex")
public class BackIndexController {
    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    /**
     * 后台首页
     *
     * @param model model
     * @param session session
     * @return 后台首页
     */
    @RequestMapping("/homepage")
    public String index(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        // 用户查询自己的文章, 管理员查询所有的
        if (user.getState() != 1) {
            //文章列表
            List<Post> postList = postService.listRecentPostById(userId, 10);
            model.addAttribute("postList", postList);

            //评论列表
            List<Comment> commentList = commentService.listRecentCommentById(userId, 10);
            model.addAttribute("commentList", commentList);
        } else {
            //文章列表
            List<Post> postList = postService.listRecentPost(10);
            model.addAttribute("postList", postList);

            //评论列表
            List<Comment> commentList = commentService.listRecentComment(10);
            model.addAttribute("commentList", commentList);
        }
        return "Admin/index";
    }
}
